package com.wyh.lotterysystem.controller;

import com.wyh.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.wyh.lotterysystem.common.exception.ControllerException;
import com.wyh.lotterysystem.common.pojo.CommonResult;
import com.wyh.lotterysystem.common.utils.JacksonUtil;
import com.wyh.lotterysystem.controller.request.ActivityDetailRequest;
import com.wyh.lotterysystem.controller.request.ActivityListRequest;
import com.wyh.lotterysystem.controller.request.ActivityPrize;
import com.wyh.lotterysystem.controller.request.CreateActivityRequest;
import com.wyh.lotterysystem.controller.response.ActivityDetailResponse;
import com.wyh.lotterysystem.controller.response.ActivityListResponse;
import com.wyh.lotterysystem.controller.response.CreateActivityResponse;
import com.wyh.lotterysystem.service.ActivityService;
import com.wyh.lotterysystem.service.dto.ActivityDetailDTO;
import com.wyh.lotterysystem.service.dto.ActivityListDTO;
import com.wyh.lotterysystem.service.dto.CreateActivityDTO;
import com.wyh.lotterysystem.service.dto.PageDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
@Slf4j
public class ActivityController {
    @Resource(name = "activityServiceImpl")
    ActivityService activityService;

    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResponse> createActivity(
            @RequestBody @Validated CreateActivityRequest request){
        //打印日志
        log.info("创建活动，request{}", JacksonUtil.writeValueAsString(request));

        //调用service代码
        CreateActivityDTO dto =  activityService.createActivity(request);

        //返回
        return CommonResult.success(new CreateActivityResponse(dto.getActivityId()));
    }

    @RequestMapping("/activity/find-list")
    public CommonResult<ActivityListResponse> getActivityList(ActivityListRequest request){
        log.info("获取活动列表，page:{},offset{}",request.getCurrentPage(),request.offset());

        PageDTO<ActivityListResponse.ActivityList> activityListDTO = activityService.getActivityList(request);

        ActivityListResponse response = covertToActivityListResponse(activityListDTO);

        return CommonResult.success(response);
    }

    private ActivityListResponse covertToActivityListResponse(PageDTO<ActivityListResponse.ActivityList> activityListDTO) {
        if (null == activityListDTO){
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_LIST);
        }

        ActivityListResponse activityListResponse = new ActivityListResponse();
        activityListResponse.setTotal(activityListDTO.getTotal());

        activityListResponse.setRecords(activityListDTO.getRecord());
        return activityListResponse;
    }

    @RequestMapping("/activity-detail/find")
    public CommonResult<ActivityDetailResponse> findActivityDetail(Integer activityId){
        log.info("获取活动详细信息, activityId:{}",activityId);

        ActivityDetailDTO activityDetailDTO = activityService.findActivityDetail(activityId);

        return CommonResult.success(covertToActivityDetailResponse(activityDetailDTO));

    }

    private ActivityDetailResponse covertToActivityDetailResponse(ActivityDetailDTO activityDetailDTO) {
        if (null == activityDetailDTO){
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }

        ActivityDetailResponse response = new ActivityDetailResponse();

        response.setActivityId(activityDetailDTO.getActivityId());
        response.setActivityName(activityDetailDTO.getActivityName());
        response.setDescription(activityDetailDTO.getDesc());
        response.setStatus(activityDetailDTO.getStatus());
        response.setValid(activityDetailDTO.valid());

        List<ActivityDetailDTO.PrizeDTO> prizeDTOS = activityDetailDTO.getPrizeDTOList()
                .stream()
                .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getPrizeTierName().getCode()))
                .map(prizeDTO -> {
                    ActivityDetailDTO.PrizeDTO prizeDTO1 = new ActivityDetailDTO.PrizeDTO();
                    prizeDTO1.setPrizeId(prizeDTO.getPrizeId());
                    prizeDTO1.setPrizeAmount(prizeDTO.getPrizeAmount());
                    prizeDTO1.setDescription(prizeDTO.getDescription());
                    prizeDTO1.setName(prizeDTO.getName());
                    prizeDTO1.setPrice(prizeDTO.getPrice());
                    prizeDTO1.setPrizeTierName(prizeDTO.getPrizeTierName());
                    prizeDTO1.setImageUrl(prizeDTO.getImageUrl());
                    prizeDTO1.setStatus(prizeDTO.getStatus());
                    prizeDTO1.setValid(prizeDTO.valid());

                    return prizeDTO1;
                })
                .toList();
        response.setPrizes(prizeDTOS);

        List<ActivityDetailDTO.UserDTO> userDTOS = activityDetailDTO.getUserDTOList()
                .stream()
                .map(userDTO -> {
                    ActivityDetailDTO.UserDTO userDTO1 = new ActivityDetailDTO.UserDTO();
                    userDTO1.setUserName(userDTO.getUserName());
                    userDTO1.setUserId(userDTO.getUserId());
                    userDTO1.setStatus(userDTO.getStatus());
                    userDTO1.setValid(userDTO1.valid());

                    return userDTO1;
                })
                .toList();
        response.setUsers(userDTOS);

        return response;
    }


}
